package com.small;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.small.view.custview.dialog.PopFirstAdapter;
import com.small.view.custview.dialog.SingleChoicePopWindow;

public class InitActivity extends Activity {
	Button btn1,btn2;
	protected SingleChoicePopWindow singleChoicePopWindow;
	
	private View view; 
	private PopupWindow mPopupWindow;
	
	ListView list_view1;
	ListView list_view2;
	PopFirstAdapter list_ada;
	List<String> list_model;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 
		setContentView(R.layout.test);
		this.singleChoicePopWindow = new SingleChoicePopWindow(InitActivity.this);
		btn1 = (Button)this.findViewById(R.id.button1);
		btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 showChoicePopWindow(null, null);
				
			}
		});
		
		btn2 = (Button)this.findViewById(R.id.button2);
		btn2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				showPopupWindow(btn2);
				
			}
		});
		initPopupWindow(R.layout.choice_single_list_layout);
	} 
	

	public void showChoicePopWindow(String[] itmes, String title) {
		if (!singleChoicePopWindow.isShowing()) {
			singleChoicePopWindow.initChoicePopWindow(itmes, title);
			singleChoicePopWindow.show();
		}
	}

	public void dismissChoicePopWindow() {
		if (singleChoicePopWindow.isShowing()) {
			singleChoicePopWindow.dismiss();
		}
	}
	
	
	private void initPopupWindow(int resId){
		LayoutInflater mLayoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
	    view = mLayoutInflater.inflate(resId, null);
	        
		mPopupWindow = new PopupWindow(view,LayoutParams.WRAP_CONTENT,280);
//		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());//必须设置background才能消失
		mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_frame));
		mPopupWindow.setOutsideTouchable(true);
		
		//自定义动画
//		mPopupWindow.setAnimationStyle(R.style.PopupAnimation);
		//使用系统动画
//		mPopupWindow.setAnimationStyle(android.R.style.Animation_Translucent);
		mPopupWindow.setFocusable(true);
		mPopupWindow.update();
		mPopupWindow.setTouchable(true); 
		
		list_view1=(ListView)view.findViewById(R.id.listView1);
		list_view2=(ListView)view.findViewById(R.id.list2);
		list_view1.setFocusable(true);
		list_view2.setFocusable(true);
		list_view1.setSelection(0);
		
		list_view1.setFocusableInTouchMode(true);
		list_view2.setFocusableInTouchMode(true);
		list_model= new ArrayList<String>();
		list_model.add("dde");
		list_model.add("dde");
		list_model.add("dde");
		list_model.add("dde");
		list_model.add("dde");
		list_model.add("dde");
		list_ada=new PopFirstAdapter(this, list_model,1);
		list_view1.setAdapter(list_ada);
		list_view2.setAdapter(list_ada);
		
		list_view1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				 Toast.makeText(InitActivity.this, "一级"+arg2, Toast.LENGTH_LONG).show();
				
			}
		});
		
		list_view2.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				 Toast.makeText(InitActivity.this, "二级"+arg2, Toast.LENGTH_LONG).show();
				
			}
		}); 
	}
	private void showPopupWindow(View view) {
		if(!mPopupWindow.isShowing()){
			mPopupWindow.showAsDropDown(view,0,0);
//			mPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
		}
	} 
	
}
